// controllers/homeController.js
import Home from "../models/homePage.js";
import { successResponse, errorResponse } from "../utils/responseHelper.js";
import Concern from "../models/concern.js";

export const getHomes = async (ctx) => {
  try {
    const homes = await Home.findAll();
    successResponse(ctx, homes);
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};

export const getHomeById = async (ctx) => {
  console.log(ctx);
  const { id } = ctx.params;
  // 从请求头中获取 username
  const username = ctx.request.headers.username;
  console.log(username);
  const data = await Concern.findOne({
    where: { user_con: id, user: username },
  });
  console.log(data, "-----------------------------------------");
  const isConcern = data ? true : false;
  console.log(id);
  try {
    const home = await Home.findOne({ where: { id } });
    if (home) {
      successResponse(ctx, { home, isConcern });
    } else {
      successResponse(ctx, "Not found");
    }
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};
export const getHomeByCategories = async (ctx) => {
  console.log(ctx);
  const { categories } = ctx.params;
  console.log(categories);
  try {
    const home = await Home.findAll({
      where: {
        categories: categories, // 根据categories字段查询
      },
    });
    if (home) {
      successResponse(ctx, home);
    } else {
      successResponse(ctx, "Not found");
    }
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};

export const addHomePageData = async (ctx) => {
  const {
    name,
    head_shot,
    image,
    hot,
    info,
    video,
    dynamic_info,
    dynamic_video,
    categories,
  } = ctx.request.body;
  try {
    const home = await Home.create({
      name,
      head_shot,
      image,
      hot,
      info,
      video,
      dynamic_info,
      dynamic_video,
      categories,
    });
    if (home) {
      successResponse(ctx, home);
    } else {
      successResponse(ctx, "Not found");
    }
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};

export const deleteHomeById = async (ctx) => {
  const { id } = ctx.params;
  try {
    const home = await Home.destroy({ where: { id } });
    const concern = await Concern.destroy({ where: { user_con: id } });
    if (home) {
      successResponse(ctx, home);
    } else {
      successResponse(ctx, "Not found");
    }
  } catch (error) {
    errorResponse(ctx, 500, "error");
  }
};
export const updateHomePageData = async (ctx) => {
  const {
    id,
    name,
    head_shot,
    image,
    hot,
    info,
    video,
    dynamic_info,
    dynamic_video,
    categories,
  } = ctx.request.body;

  try {
    const [updated] = await Home.update(
      {
        name,
        head_shot,
        image,
        hot,
        info,
        video,
        dynamic_info,
        dynamic_video,
        categories,
      },
      {
        where: { id },
      }
    );

    if (updated) {
      const updatedHome = await Home.findByPk(id);
      successResponse(ctx, updatedHome);
    } else {
      successResponse(ctx, "Not found");
    }
  } catch (error) {
    console.error(error);
    errorResponse(ctx, 500, "Error updating entry");
  }
};
